<?php
namespace app\bbs\controller;
use x\my\Pay;
class Userxiangmu extends Base{
  public function __construct(){
    parent::__construct();
    $this->checkLogin();
  }
  //微信支付页面, 扫码支付
  public function pay(){
    if(is_post()){
        $d = $this->post;
        $rule = [
          'year'=>'require',
          'price'=>'require',
          'id'=>'require',
        ];
        $msg = [
        ];
        check($rule,$msg,$d);
        $info = $this->db->find('xiangmu',$d['id']);
        $order_no = $this->com_order_no();
        //价格
        $money = $info['price'.$d['price']];
        if(!$money)err('价格错误');
        $fee = bcmul($money, c('type.fee'),2);
        $real_money = bcsub($money, $fee, 2);
        $data = [
          'order_no' => $order_no,
          'user_id' => $this->userinfo['id'],
          'year' => $d['year'],
          'xiangmu_id' => $d['id'],
          'money' => $money,
          'fee' => $fee,
          'real_money' => $real_money,
          'create_time' => time(),
        ];
        $id = $this->db->insert('order_xiangmu', $data);

        //微信支付
        $con = c('api.wx_gzh_config.myb');
        $config = [
            'app_id'  => $con['app_id'],
            'mch_id'  => $con['mch_id'],
            'key'     => $con['key'],
            'mode'    => 'optional',
            'notify_url' => url('notice/wxpay'),
        ];
        $pay = Pay::wechat($config);
        $order = [
            'out_trade_no' => $order_no,
            'body' => '购买蚂蚁帮在线服务',
            'total_fee' => bcmul($money,100),
        ];
        $rs = $pay->scan($order);
        $txt = urlencode($rs->code_url);
        $url = url('com/qrcode',['txt'=>$txt]);
        suc(['url'=>$url, 'order_no'=>$order_no]);
    }
    //官方群
    $patch = m('patch','c')->getData(['qun']);
    $info = $this->db->find('xiangmu',$this->get('id'));
    $this->fetch(['info'=>$info,'patch'=>$patch]);
  }

  //轮询支付
  public function querypay(){
    if(is_post()){
        $d = $this->post;
        $rule = [
            'order_no'=>'require',
        ];
        $msg = [
        ];
        check($rule,$msg,$d);
        $rs = $this->db->query("select xiangmu_id from order_xiangmu where order_no=? and pay_status=?", [$d['order_no'],1],1);
        if($rs){
          $id = $rs['xiangmu_id'];
          $info = $this->db->find('xiangmu',$id);
          $url = $info['admin_url'];
          suc($url);
        }else{
          err('未支付');
        }
    }
  }
}
